# coding=UTF-8

from syn.base import syn_dept, syn_user, syn_tenant
from syn.eqm import syn_sample_box, sys_device_model, sys_sheleves_type, sys_box_type, syn_room, syn_device, \
    syn_eqm_layer, syn_eqm_shelf, syn_eqm_box, generate_drawer, update_eqm_freeze_box_drawer_rel, sys_device_type
from syn.project import sys_project, sys_project_sample_type, syn_project_type, sys_project_ex_value, syn_project_team, \
    syn_project_member
from syn.sample import syn_sample, syn_sample_ex_value, syn_sample_sn_resource_project, syn_sample_type, \
    syn_sample_type_attr
from syn.sampleResource import syn_sample_resource, syn_sample_resource_ex_value, sys_sample_ex_and_resource_ex_dict, \
    syn_sample_resource_type, syn_sample_resource_type_attr
from syn.standard import syn_dict, sys_unit, syn_cus_field
from tools.dbo import get_mysql_conn, get_sql_server_conn


def syn_base_data(sql_server_conn, mysql_conn):
    # 系统基础数据迁移 =======start=======
    # 部门导入
    syn_dept(sql_server_conn, mysql_conn)
    # 人员相关数据迁移
    syn_user(sql_server_conn, mysql_conn)
    # 租户生成
    syn_tenant(sql_server_conn, mysql_conn)
    # 字典迁移
    syn_dict(sql_server_conn, mysql_conn)
    # 单位unit迁移
    sys_unit(sql_server_conn, mysql_conn)
    # 项目分类迁移
    syn_project_type(sql_server_conn, mysql_conn)
    # 自定义字段迁移
    syn_cus_field(sql_server_conn, mysql_conn)
    # 样本类型迁移
    syn_sample_type(sql_server_conn, mysql_conn)
    # 样本类型属性迁移
    syn_sample_type_attr(sql_server_conn, mysql_conn)
    # 样本源类型迁移
    syn_sample_resource_type(sql_server_conn, mysql_conn)
    # 样本源扩展属性迁移
    syn_sample_resource_type_attr(sql_server_conn, mysql_conn)
    # 系统基础数据迁移 =======end=======

def syn_device_base_data(sql_server_conn,mysql_conn):
    # 设备相关基础数据迁移 =======start=======
    # 设备类型迁移
    sys_device_type(sql_server_conn, mysql_conn) #无对应数据表
    # 设备型号迁移
    sys_device_model(sql_server_conn, mysql_conn)
    # 冰冻架类型迁移
    sys_sheleves_type(sql_server_conn, mysql_conn)
    # 冰冻盒类型迁移
    sys_box_type(sql_server_conn, mysql_conn)
    # 设备相关基础数据迁移 =======end=======

def syn_device_business_data(sql_server_conn,mysql_conn):
    # 设备相关业务数据迁移 =======start=======
    # 房间信息迁移
    syn_room(sql_server_conn, mysql_conn)
    # 设备信息迁移
    syn_device(sql_server_conn, mysql_conn)
    # 层级迁移
    syn_eqm_layer(sql_server_conn, mysql_conn)
    # 架子迁移
    syn_eqm_shelf(sql_server_conn, mysql_conn)
    # 盒子迁移
    syn_eqm_box(sql_server_conn, mysql_conn)
    # 生成抽屉数据
    generate_drawer(sql_server_conn, mysql_conn)
    # 更新盒子抽屉关联关系
    update_eqm_freeze_box_drawer_rel(sql_server_conn, mysql_conn)
    # 设备相关业务数据迁移 =======end=======

def syn_sample_data(sql_server_conn, mysql_conn):
    # 样本相关数据迁移 =======start=======
    # 样本源迁移
    syn_sample_resource(sql_server_conn, mysql_conn)
    # 样本源自定义字段迁移
    syn_sample_resource_ex_value(sql_server_conn, mysql_conn)
    # 样本数据迁移
    syn_sample(sql_server_conn, mysql_conn)
    # 样本自定义字段迁移
    syn_sample_ex_value(sql_server_conn, mysql_conn)
    # 样本项目同步样本源项目
    syn_sample_sn_resource_project(sql_server_conn, mysql_conn)
    # 盒子与样本关联关系
    syn_sample_box(sql_server_conn, mysql_conn)
    # 修改样本源拓展字段，以及修改样本拓展字段字典项 code->id-value
    sys_sample_ex_and_resource_ex_dict(sql_server_conn, mysql_conn)
    # 样本相关数据迁移 =======end=======

def syn_project_data(sql_server_conn, mysql_conn):
    # 项目相关数据迁移 =======start=======
    # 项目信息迁移
    sys_project(sql_server_conn, mysql_conn)
    # 项目附加信息迁移
    sys_project_ex_value(sql_server_conn, mysql_conn)
    # 项目成员信息迁移
    syn_project_team(sql_server_conn, mysql_conn)
    # 项目样本类型关系同步
    sys_project_sample_type(sql_server_conn, mysql_conn)
    # 项目相关数据迁移 =======end=======

# 执行方法控制器
def implement_method(sql_server_conn, mysql_conn):

    print("===============================开始迁移数据==============================")

    syn_base_data(sql_server_conn,mysql_conn)

    syn_device_base_data(sql_server_conn, mysql_conn)

    syn_device_business_data(sql_server_conn, mysql_conn)

    syn_project_data(sql_server_conn, mysql_conn)

    syn_sample_data(sql_server_conn, mysql_conn)

    print("===============================数据迁移完成==============================")



# 主要函数
if __name__ == '__main__':
    # 新建mysql连接
    mysql_conn = get_mysql_conn("10.182.2.155", 3308, "root", "Haier123456", "whtj6")
    # 新建sql server连接
    sql_server_conn = get_sql_server_conn("118.190.2.209", 6666, "BIMSTEMP0122", "WE#9O455dZ10b", "BIMSTEMP0122")
    # 调用同步方法
    # start = time.time()
    implement_method(sql_server_conn, mysql_conn)


